# Zhou-Horiuchi, Study 1

# Initial settings --------------------------------------------------------

dir.create("output/study1", recursive = TRUE, showWarnings = FALSE)
dir.create("figures/study1", recursive = TRUE, showWarnings = FALSE)
dir.create("logs", recursive = TRUE, showWarnings = FALSE)

source("functions/logging.R")
start_script_log("study1")

options(tidyverse.quiet = TRUE)
library(tidyverse)
library(estimatr)
library(ggthemes)
library(patchwork)

# Read and wrangle data ---------------------------------------------------

source("functions/read_Qualtrics.R")
source("functions/reshape_conjoint.R")
source("functions/wrangle_data.R")

out <- wrangle_data("study1")
cat("\n------------------------------------------------------------\n")
cat("Saved data: output/study1/data_for_analysis.rds\n")
cat("------------------------------------------------------------\n\n")

# Test pre-registered hypotheses ------------------------------------------

source("functions/test_hypotheses.R")
test_hypotheses(out[[2]])[[1]]
ggsave("figures/study1/pre-registered_hypotheses.pdf", width = 8, height = 4)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/pre-registered_hypotheses.pdf\n")
cat("------------------------------------------------------------\n\n")

# Visualize profile-level marginal means ----------------------------------

# Note: This is not the main analysis we pre-registered. Here, we calculate
# the marginal means using the standard approach (the unit of analysis is
# each profile). But our pre-registered analysis use each profile pair as
# the unit of analysis.

source("functions/visualize_MMs.R")
visualize_MMs(out[[1]])
ggsave("figures/study1/profile_level_MMs.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/profile_level_MMs.pdf\n")
cat("------------------------------------------------------------\n\n")

# Further exploration -----------------------------------------------------

source("functions/make_figure.R")
source("functions/expand_profile_data.R")
profile_pairs_expanded <- expand_profile_data(out[[2]], out[[3]]) %>%
  mutate(zainichi_region = case_when(
    Q9.3 %in% c("Kyoto", "Hyogo", "Osaka") ~ TRUE,
    .default = FALSE
  ))

make_figure(
  profile_pairs_expanded,
  quo(resentment_tercile),
  "Resentment",
  "study1"
)

ggsave("figures/study1/estimates_by_resentment.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_resentment.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(Q9.4),
  "Respondent Gender"
)
ggsave("figures/study1/estimates_by_respondent_gender.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_resentment.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(age_range),
  "Respondent Age"
)
ggsave("figures/study1/estimates_by_respondent_age.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_respondent_age.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(college_grad),
  "Respondent Education"
)
ggsave("figures/study1/estimates_by_respondent_education.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_respondent_education.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(political_affiliation),
  "Respondent Political Leaning"
)
ggsave("figures/study1/estimates_by_political_leaning.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_political_leaning.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded %>%
    filter(Q9.3 %in% c("Osaka", "Kyoto", "Hyogo")),
  quo(Q9.3),
  "Respondent Region"
)
ggsave("figures/study1/estimates_by_three_regions.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_three_regions.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(nationalism_tercile),
  "Respondent Political Leaning"
)
ggsave("figures/study1/estimates_by_nationalism.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_nationalism.pdf\n")
cat("------------------------------------------------------------\n\n")

make_figure(
  profile_pairs_expanded,
  quo(genders),
  "Hypothetical Applicant Gender"
)
ggsave("figures/study1/estimates_by_applicant_gender.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/study1/estimates_by_applicant_gender.pdf\n")
cat("------------------------------------------------------------\n\n")


end_script_log()
